<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * SAEF controller for the users module (frontend)
 *
 * @package 		PyroCMS
 * @subpackage 		Blog SAEF module
 * @category		Modules
 * @author			Rick Holmes 
 * @version			1.0 
 */
class Saef extends Public_Controller {
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('saef/saef_m');
		$this->load->model('blog/blog_m');
		
		$this->load->library('form_validation');
		
		$this->lang->load('saef');
		
		$this->saef_validation_rules = array(
			array(
				'field' => 'category',
				'label' => lang('saef:post_category_label'),
				'rules' => 'required'
			),
			array(
				'field' => 'title',
				'label' => lang('saef:post_title_label'),
				'rules' => 'trim|required'	
			),
			array(
				'field' => 'body',
				'label' => lang('saef:post_body_label'),
				'rules' => 'trim|required'
			)
		);
	}
	
	/** the Ajax handler for the SAEF widget
	 *
	 * @access public (Ajax only)	 
	 * @param array ($_POST)
	 * @uses blog/blog_m->check_exists()	 
	 * @uses $this->_create_slug()
	 * @uses $this->_notify_admin()	 	 
	 * @return string (echo)
	 */	 	 	 	
	public function post()
	{
		// ajax only!	
		if (!$this->input->is_ajax_request()) die('no browser access');
				
		$this->form_validation->set_rules($this->saef_validation_rules);
		
		if ($this->form_validation->run())
		{
			$title = $this->input->post('title');
						
			if ($this->blog_m->check_exists('title', $title))
			{
				//$slug = $this->_create_slug($title);
				$slug = url_title($title, 'dash', TRUE); 
				
				// check for require_moderation
				$require_moderation = (bool) $this->saef_m->requires_moderation(); 
				
				$id = $this->blog_m->insert(array(
					'title'				=> $title,
					'slug'				=> $slug,
					'category_id'		=> $this->input->post('category'),
					'keywords'			=> '',
					'intro'				=> $this->input->post('introduction'),
					'body'				=> $this->input->post('body'),
					'status'			=> $require_moderation ? 'draft' : 'live',
					'created_on'		=> now(),
					'comments_enabled'	=> 0,
					'author_id'			=> $this->current_user->id,
					'type'				=> 'wysiwyg-simple',
					'parsed'			=> ''
				));
				
				if ($id)
				{
					$this->pyrocache->delete_all('blog/blog_m');
					if (!$require_moderation) //  || $this->ion_auth->is_admin()
					{
						$link = 'blog/' . date('Y') . '/' . date('m') . '/' . $slug;
						echo sprintf(lang('saef:publish_success'), $title, site_url($link));
					}
					else
					{
						$cat_title = $this->saef_m->get_category_title($this->input->post('category'));
						
						if ($this->_notify_admin($title, $cat_title))
						{
							echo sprintf($this->lang->line('saef:publish_moderate'), $title);
						}
						else
						{
							echo lang('saef_error_moderation_email');
						}
					}
				}
				else
				{
					echo 'Error: ' . $this->lang->line('saef:error');
				} 
			}
			else
			{	
				echo sprintf(lang('saef:error_already_exists'), $title);
				exit();
			}
				
		}
		else
		{
			echo 'Error: ' . validation_errors();
		}	
	}
	
	/**
	 * check to see if the user is the author of the post in question
	 * 
	 * @param string ($_POST)
	 * @return/echo string
	 */	 	 	 	 	
	public function check_author_login()
	{
		// ajax only!	
		if (!$this->input->is_ajax_request()) die('no browser access');
		
		// get the author of the post
		$slug = $this->input->post('slug');
		$author_id = $this->saef_m->get_entry_author($slug);
		
		// is he logged in?
		if ((!empty($this->current_user) && $this->current_user->active == 1) || $this->current_user->group == 1)
		{
			if($this->current_user->id == $author_id) 
			{
				echo 'ok';
				exit();	
			}
		}
		echo 'not the mama!';
		exit();
	}
	
	public function get_settings()
	{
		if (!$this->input->is_ajax_request()) die('no browser access');
		
		$settings = $this->saef_m->get_saef_settings();
		
		if ($settings->allow_editing)
		{
			echo $settings->editable_div;
			exit();
		}
		echo 'false';
		exit();
	}
	
	
	/**
	 * change the post content in the db
	 * 
	 * @param string (area of post: intro or body)
	 * @param string ($_POST['content']
	 * @param string ($_POST['slug'])
	 * @return/echo string
	 */	 	 	 	 	 	 	
	public function edit($area)
	{
		// ajax only!	
		if (!$this->input->is_ajax_request()) die('no browser access');
		
		$content = $this->input->post('content', TRUE);
		$slug = $this->input->post('slug');
		
		//echo 'ok'; exit();
		if ($this->saef_m->update_blog_content($slug, $area, $content))
		{
			echo 'ok';
		}
		else
		{
			echo 'error';	
		}
		exit();
	}
	
	/**
	 * create a slug from the post title
	 *
	 * @access private	  
	 * @param string
	 * @return string
	 */	 	 	 	 	
	private function _create_slug($str)
	{
		$this->load->helper('text');

        return trim(url_title($str, 'dash', TRUE), '-');
	}
	
	/**
	 * email the first admin found that a post needs moderation
	 *
	 * @access private	  
	 * @param string
	 * @param string
	 * @return bool
	 */	 	 	 	 	 	
	private function _notify_admin($title, $cat_name)
	{
		$admin_data = $this->saef_m->get_admin_data();
		
		$this->load->library('email');

		$this->email->to($admin_data->email);
		$this->email->from($this->current_user->email, $this->current_user->display_name);
		$this->email->subject('New post awaiting moderation');
		
		$message = sprintf(lang('saef:email_description'), $this->current_user->display_name, $cat_name, $title) . "\r\n";
		$message .= lang('saef:email_action');
		$this->email->message($message);
		
		return $this->email->send();		
	}	
}

/* end of file saef.php */
/* location /addons/shared_addons/modules/saef/controllers/saef.php */